# -*- coding: utf-8 -*-
"""
@Time ： 2025/8/28 15:37
@Auth ： 李天天
"""
import time

from PageLocators.PlayerPage_locator import PlayPageLocator as loc

from Common.basepage import BasePage


class PlayerPage(BasePage):
    def click_seekbar(self):
        # 若进度条不可见，点击屏幕中心唤起控件
        if not self.ensure_element_visible_with_center_wakeup(loc.seekbar_loc):
            self.logger.warning("进度条唤起失败，仍尝试点击一次")
        self.click_element(loc.seekbar_loc, img_desc="播放页面点击_进度条")

    def click_seekbar_by_percentage(self, percent: float) -> bool:
        """
        按百分比点击进度条，例如0.1(10%)、0.2(20%)、0.8(80%)。
        :param percent: 0.0-1.0 之间的小数
        """
        # 若进度条不可见，点击屏幕中心唤起控件
        if not self.ensure_element_visible_with_center_wakeup(loc.seekbar_loc):
            self.logger.warning("进度条唤起失败，仍尝试按百分比点击一次")
        # 进度条通常竖向居中点击，x轴使用百分比，y轴使用0.5
        return self.click_element_by_percentage(
            loc.seekbar_loc,
            x_percent=percent,
            y_percent=0.5,
            img_desc=f"播放页面点击_进度条_{int(percent * 100)}%"
        )

    def click_back(self):
        # 若返回按钮不可见，点击屏幕中心唤起控件
        if not self.ensure_element_visible_with_center_wakeup(loc.back_loc):
            self.logger.warning("返回按钮唤起失败，仍尝试点击一次")
        self.click_element(loc.back_loc, img_desc="播放页面点击_返回按钮")

    def click_chioce_episode(self):
        if not self.ensure_element_visible_with_center_wakeup(loc.back_loc):
            self.logger.warning("返回按钮唤起失败，仍尝试点击一次")
        self.click_by_text("选集", tag="iqp", img_desc="播放页面点击_选集")

    def click_episode(self, index):
        self.click_element(loc.episode_loc.replace("index", str(index)), img_desc=f"播放页面点击_{index}_选集")

    def click_chioce_rate(self):
        if not self.ensure_element_visible_with_center_wakeup(loc.rate_choice_loc):
            self.logger.warning("返回按钮唤起失败，仍尝试点击一次")
        # self.click_by_text("倍速", tag="iqp", img_desc="播放页面点击_倍速")
        self.click_element(loc.rate_choice_loc, img_desc="播放页面点击_倍速")

    def click_rate(self, rate):
        self.click_element(loc.rate_loc.replace("rate", rate), img_desc=f"播放页面点击_{rate}_倍速")

    def click_chioce_bit(self):
        if not self.ensure_element_visible_with_center_wakeup(loc.bit_choice_loc):
            self.logger.warning("返回按钮唤起失败，仍尝试点击一次")
        self.click_element(loc.bit_choice_loc, img_desc="播放页面点击_清晰度")

    def click_bit(self, bit):
        self.click_element(loc.bit_loc.replace("bit", bit), img_desc=f"播放页面点击_{bit}_清晰度")


if __name__ == '__main__':
    from Common.browser_config import quick_start
    from PageObjects.Main_Page import MainPage

    pw, browser, context, page = quick_start(auto_login=True)
    do_mainpage = MainPage(page)
    do_playerpage = PlayerPage(page)
    do_mainpage.goto("https://www.iqiyi.com/iv#/?ver=tesla")
    time.sleep(5)
    do_mainpage.click_first_movie()
    time.sleep(8)
    # do_playerpage.click_seekbar_by_percentage(0.2)
    do_playerpage.click_chioce_episode()
    do_playerpage.click_episode(1)
    time.sleep(5)
    do_playerpage.click_chioce_episode()
    do_playerpage.click_episode(3)
    time.sleep(60)
